<template>
  <div class="login">
    <div class="login-left">
      <div class="app-logo">
        <img src="@/assets/logo/logo.png" alt="logo" />
        <h1>求知若饥 · 虚心若愚</h1>
      </div>
      <div class="my-auto">
        <img src="@/assets/images/login-box-bg.svg" alt="" />
        <div class="app-box">
          <span>开箱即用的中后台管理系统</span>
        </div>
        <div class="app-desc">输入您的个人详细信息开始使用！</div>
      </div>
    </div>
    <div class="login-right">
      <div class="login-box">
        <el-form key="login" ref="loginRef" :model="loginForm" :rules="loginRules" class="login-form">
          <h3 class="title">登录</h3>

          <el-form-item prop="username">
            <el-input v-model="loginForm.username" type="text" size="large" auto-complete="off" placeholder="账号">
              <template #prefix><svg-icon icon-class="user" class="el-input__icon input-icon" /></template>
            </el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input v-model="loginForm.password" type="password" size="large" auto-complete="off" placeholder="密码" @keyup.enter="handleLogin">
              <template #prefix><svg-icon icon-class="password" class="el-input__icon input-icon" /></template>
            </el-input>
          </el-form-item>
          <!-- <el-form-item prop="code" v-if="captchaEnabled">
            <el-input v-model="loginForm.code" size="large" auto-complete="off" placeholder="验证码" style="width: 63%" @keyup.enter="handleLogin">
              <template #prefix><svg-icon icon-class="validCode" class="el-input__icon input-icon" /></template>
            </el-input>
            <div class="login-code">
              <img :src="codeUrl" @click="getCode" class="login-code-img" />
            </div>
          </el-form-item> -->
          <el-form-item class="login-remember">
            <el-checkbox v-model="loginForm.rememberMe">记住密码</el-checkbox>
            <span class="forget-password" @click="forgetPassword">忘记密码?</span>
          </el-form-item>

          <el-form-item style="width:100%;">
            <el-button :loading="loading" size="large" type="primary" style="width:100%;" @click.prevent="handleLogin">
              <span v-if="!loading">登 录</span>
              <span v-else>登 录 中...</span>
            </el-button>
          </el-form-item>
          <el-form-item class="register">
            <!--<el-button @click="phoneLogin">手机登录</el-button>-->
            <!--<el-button @click="qrcodeLogin">二维码登录</el-button>-->
            <el-button size="large" type="primary" style="width:100%;" @click="registerClick">
              <span>注 册</span>
            </el-button>
          </el-form-item>
          <!--<div class="separator">-->
          <!--  <span class="inner-text">其他登录方式</span>-->
          <!--</div>-->
          <!--<el-form-item class="social">-->
          <!--  <el-button circle title="微信登录" @click="doSocialLogin('wechat')">-->
          <!--    <svg-icon icon-class="wechat" />-->
          <!--  </el-button>-->
          <!--  <el-button circle title="MaxKey登录" @click="doSocialLogin('maxkey')">-->
          <!--    <svg-icon icon-class="maxkey" />-->
          <!--  </el-button>-->
          <!--  <el-button circle title="Gitee登录" @click="doSocialLogin('gitee')">-->
          <!--    <svg-icon icon-class="gitee" />-->
          <!--  </el-button>-->
          <!--  <el-button circle title="Github登录" @click="doSocialLogin('github')">-->
          <!--    <svg-icon icon-class="github" />-->
          <!--  </el-button>-->
          <!--</el-form-item>-->
        </el-form>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { getCodeImg } from '@/api/login';
import { authBinding } from '@/api/system/social/auth';
import Cookies from 'js-cookie';
import { useUserStore } from '@/store/modules/user';
import { LoginData } from '@/api/types';
import { to } from 'await-to-js';
import { HttpStatus } from "@/enums/RespEnum";
import { encrypt } from "@/utils/jsencrypt";

const userStore = useUserStore();
const router = useRouter();

const loginForm = ref<LoginData>({
  username: 'admin',
  password: 'admin123',
  rememberMe: false,
} as LoginData);

const loginRules: ElFormRules = {
  tenantId: [{ required: true, trigger: "blur", message: "请输入您的租户编号" }],
  username: [{ required: true, trigger: 'blur', message: '请输入您的账号' }],
  password: [{ required: true, trigger: 'blur', message: '请输入您的密码' }],
  code: [{ required: true, trigger: 'change', message: '请输入验证码' }]
};

const codeUrl = ref('');
const loading = ref(false);
// 验证码开关
const captchaEnabled = ref(true);


// 注册开关
const redirect = ref(undefined);
const loginRef = ref<ElFormInstance>();

const handleLogin = () => {
  loginRef.value?.validate(async (valid: boolean, fields: any) => {
    if (valid) {
      loading.value = true;
      //RSA密码加密传输
      loginForm.value.password = encrypt(loginForm.value.password as string) as string
      // 勾选了需要记住密码设置在 cookie 中设置记住用户名和密码
      if (loginForm.value.rememberMe) {
        Cookies.set('username', String(loginForm.value.username), { expires: 30 });
        Cookies.set('password', String(loginForm.value.password), { expires: 30 });
        Cookies.set('rememberMe', String(loginForm.value.rememberMe), { expires: 30 });
      } else {
        // 否则移除
        Cookies.remove('username');
        Cookies.remove('password');
        Cookies.remove('rememberMe');
      }
      // 调用action的登录方法
      const [err] = await to(userStore.login(loginForm.value));
      if (!err) {
        await router.push({ path: redirect.value || '/' });
      } else {
        loading.value = false;
      }
    } else {
      console.log('error submit!', fields);
    }
  });
};

/** 去注册 */
const registerClick = () => {
  router.push('/register')
}

/**
 * 获取验证码
 */
const getCode = async () => {
  const res = await getCodeImg();
  const { data } = res;
  captchaEnabled.value = data.captchaEnabled === undefined ? true : data.captchaEnabled;
  if (captchaEnabled.value) {
    codeUrl.value = 'data:image/gif;base64,' + data.img;
    loginForm.value.uuid = data.uuid;
  }
};

const getCookie = () => {
  const username = Cookies.get('username');
  const password = Cookies.get('password');
  const rememberMe = Cookies.get('rememberMe');
  loginForm.value = {
    username: username === undefined ? String(loginForm.value.username) : username,
    password: password === undefined ? String(loginForm.value.password) : String(password),
    rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
  } as LoginData;
}

/**
 * 第三方登录
 * @param type
 */
const doSocialLogin = (type: string) => {
  authBinding(type).then((res: any) => {
    if (res.code === HttpStatus.SUCCESS) {
      // 获取授权地址跳转
      window.location.href = res.data;
    } else {
      ElMessage.error(res.msg);
    }
  });
};

// 二维码登录

const qrcodeLogin = () => {
  ElMessage.error('暂不支持二维码登录');
};

// 手机登录
const phoneLogin = () => {
  ElMessage.error('暂不支持手机登录');
};

// 忘记密码
const forgetPassword = () => {
  ElMessage.error('暂不支持忘记密码');
};


onMounted(() => {
  getCookie();
});
</script>

<style lang="scss" scoped>
.login {
  position: relative;
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  background-size: cover;
  padding: 1.5rem 5rem;
  box-sizing: border-box;

  &::before {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    margin-left: -48%;
    background-image: url(http://static-vben.yudao.iocoder.cn/assets/login-bg-b9f5c736.svg);
    background-position: 100%;
    background-repeat: no-repeat;
    background-size: auto 100%;
    content: "";
    z-index: -1;
  }

  .login-left {
    width: 50%;
    height: 100%;

    .app-logo {
      display: flex;
      width: 60%;
      height: 80px;
      position: absolute;
      top: 12px;
      align-items: center;
      padding-left: 7px;
      cursor: pointer;
      transition: all .2s ease;

      img {
        width: 48px;
      }

      h1 {
        font-size: 24px;
        color: #fff;
        font-weight: 700;
        transition: all .5s;
        line-height: normal;
        margin-left: 1rem;
      }
    }

    .my-auto {
      width: 100%;
      height: 100%;
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;

      img {
        width: 50%;
      }

      .app-box {
        margin-top: 3rem;

        span {
          color: #fff;
          font-size: 1.875rem;
          line-height: 2.25rem;
        }
      }

      .app-desc {
        color: #fff;
        font-size: 14px;
        margin-top: 2rem;
      }
    }
  }

  .login-right {
    width: 50%;
    height: 100%;
    display: flex;
    justify-content: center;
    align-items: center;

    .title {
      font-size: 24px;
      font-weight: bold;
      margin: 0px auto 30px auto;
      text-align: center;
      color: #707070;
    }

    .login-form {
      background: #ffffff;
      width: 400px;
      height: 370px;
      padding: 25px 25px 5px 25px;

      .el-input {
        height: 40px;

        input {
          height: 40px;
        }
      }

      .input-icon {
        height: 39px;
        width: 14px;
        margin-left: 0px;
      }
    }

    .login-tip {
      font-size: 13px;
      text-align: center;
      color: #bfbfbf;
    }

    .login-code {
      width: 33%;
      height: 40px;
      float: right;

      img {
        cursor: pointer;
        vertical-align: middle;
      }
    }

    .el-login-footer {
      height: 40px;
      line-height: 40px;
      position: fixed;
      bottom: 0;
      width: 100%;
      text-align: center;
      color: #fff;
      font-family: Arial, serif;
      font-size: 12px;
      letter-spacing: 1px;
    }

    .login-code-img {
      height: 40px;
      padding-left: 12px;
    }

    :deep(.login-remember) {
      height: 40px;
      .el-form-item__content {
        display: block;
      }
    }

    .forget-password {
      cursor: pointer;
      float: right;
      color: #0960bd;
    }

    .separator {
      display: flex;
      margin: 16px 0;
      color: #000000d9;
      font-weight: 500;
      font-size: 16px;
      white-space: nowrap;
      text-align: center;
      border-top: 0;
      border-top-color: #0000000f;
      clear: both;
      width: 100%;
      min-width: 100%;

      &::before {
        position: relative;
        top: 50%;
        width: 50%;
        border-top: 1px solid transparent;
        border-top-color: inherit;
        border-bottom: 0;
        transform: translateY(50%);
        content: "";
        border-top: 1px solid #e5e7eb;

      }

      &::after {
        position: relative;
        top: 50%;
        width: 50%;
        border-top: 1px solid transparent;
        border-top-color: inherit;
        border-bottom: 0;
        transform: translateY(50%);
        content: "";
        border-top: 1px solid #e5e7eb;
      }

      .inner-text {
        font-size: 12px;
        color: #00000073;
      }
    }

    :deep(.register) {
      .el-form-item__content {
        justify-content: space-around;
      }
    }

    :deep(.social) {
      .el-form-item__content {
        justify-content: space-around;
      }
    }
  }
}
</style>
